﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CommonFunc;

public partial class Manageweb_ManageAdmin : System.Web.UI.Page
{
    MODEL.Master MM = new MODEL.Master();
    MODEL.Master _MM = new MODEL.Master();

    BLL.Master BM = new BLL.Master();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["MasterID"] == null)
        {
            Response.Redirect("Default.aspx");
        }

        TABLE_Add.Visible = false;
        TABLE_ModPass.Visible = false;

        if (!IsPostBack)
        {
            PageDataInit();
        }
    }


    #region 页面数据初始化
    protected void PageDataInit()
    {
        try
        {
            MM.Master_ID = Convert.ToInt32(Session["MasterID"]);

            _MM = BM.GetDataByID(MM);

            LB_Role.Text = _MM.Master_Role.ToString();
        }
        catch (Exception ex)
        {
            LB_ManageAdmin.Text = ex.Message;
        }
    }
    #endregion


    #region 添加管理员 [Command] [GridView]
    protected void LBtn_Add_Command(object sender, CommandEventArgs e)
    {
        try
        {
            TABLE_Add.Visible = true;
            TB_LoginID.Text = "";
            TB_Name.Text = "";
            DDL_Role.SelectedValue = "";
            CB_IsLock.Checked = true;
        }
        catch (Exception ex)
        {
            LB_ManageAdmin.Text = ex.Message;
        }
    }
    #endregion


    #region 修改管理员密码 [Command] [GridView]
    protected void LBtn_Mod_Command(object sender, CommandEventArgs e)
    {
        try
        {
            TABLE_ModPass.Visible = true;

            LB_ID.Text = e.CommandArgument.ToString();
        }
        catch (Exception ex)
        {
            LB_ManageAdmin.Text = ex.Message;
        }
    }
    #endregion


    #region 删除管理员 [Command] [GridView]
    protected void LBtn_Del_Command(object sender, CommandEventArgs e)
    {
        try
        {
            MM.Master_ID = Convert.ToInt32(e.CommandArgument);
            MM.Master_Role = Convert.ToInt32(LB_Role.Text);

            BM.Del(MM);

            LB_ManageAdmin.Text = "删除成功";

            GV_MasterList.DataBind();
        }
        catch (Exception ex)
        {
            LB_ManageAdmin.Text = ex.Message;
        }
    }
    #endregion


    #region 添加管理员 [Click]
    protected void Btn_Add_Click(object sender, EventArgs e)
    {
        try
        {
            MM.Master_LoginID = TB_LoginID.Text;
            MM.Master_Password = CommonFunction.GetMD5string(TB_Pass.Text); 
            MM.Master_LastTime = DateTime.Now;
            MM.Master_LastIP = Request.ServerVariables["Remote_Addr"];
            MM.Master_Role = Convert.ToInt32(DDL_Role.SelectedValue);
            MM.Master_RealName = TB_Name.Text;
            MM.Master_IsLock = true;
            MM.Master_IsDel = false;

            BM.Add(MM);

            LB_ManageAdmin.Text = "添加成功";

            GV_MasterList.DataBind();
        }
        catch (Exception ex)
        {
            LB_ManageAdmin.Text = ex.Message;
        }
    }
    #endregion


    #region 修改密码 [Click]
    protected void Btn_ModPass_Click(object sender, EventArgs e)
    {
        try
        {
            MM.Master_Password = CommonFunction.GetMD5string(TB_OldPass.Text);
            MM.Master_NewPassword = CommonFunction.GetMD5string(TB_NewPass.Text);
            MM.Master_ID = Convert.ToInt32(LB_ID.Text);

            BM.ModPass(MM);

            LB_ManageAdmin.Text = "修改成功";

        }
        catch (Exception ex)
        {
            LB_ManageAdmin.Text = ex.Message;
        }
    }
    #endregion
}
